package com.company.test30;

import javax.swing.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 * Created by v on 16-12-1.
 */
public class Test30_04 extends JFrame {
    private JTextArea jta=new JTextArea();

    public static void main(String[] args){
        new Test30_04();
    }

    public Test30_04(){
        setLayout(new BorderLayout());
        add(new JScrollPane(jta),BorderLayout.CENTER);

        setTitle("MultiThreadServer");
        setSize(500,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setTitle("MultiThreadServer");
        setSize(500,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        try{
            ServerSocket serverSocket=new ServerSocket(8000);
            jta.append("MultiThreadServer started at "+
            new Date()+'\n');

            int clientNo=1;

            while (true){
                Socket socket=serverSocket.accept();

                jta.append("Starting thread for client "+
                clientNo+"at"+new Date()+'\n');

                InetAddress inetAddress=socket.getInetAddress();
                jta.append("Client "+clientNo+"'s host name is"
                    +inetAddress.getHostName()+'\n');
                jta.append("Client"+clientNo+"'s IP address is "
                    +inetAddress.getHostAddress()+'\n');

                HandleAClient task=new HandleAClient(socket);

                new Thread(task).start();

                clientNo++;
            }
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }

    class HandleAClient implements Runnable{
        private Socket socket;

        public HandleAClient(Socket socket){
            this.socket=socket;
        }

        @Override
        public void run() {
            try {
                DataInputStream inputFromClient=new
                        DataInputStream(socket.getInputStream());
                DataOutputStream outputToClient=new
                        DataOutputStream(socket.getOutputStream());

                while (true){
                    double radius=inputFromClient.readDouble();

                    double area=radius*radius*Math.PI;

                    outputToClient.writeDouble(area);
                    jta.append("radius received from client: "+
                            radius+'\n');

                    jta.append("Area found: "+area+'\n');
                }
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }
    }
}
